<?php

namespace App\Http\Controllers;

use App\Http\Requests\EnterRequest;
use App\Http\Requests\LoginRequest;
use App\Http\Requests\SmsRequest;
use App\Http\Services\LoginService;
use App\Http\Services\Sms;
use App\Models\User;
use Illuminate\Http\Request;
use EasyWeChat\OfficialAccount\Application;

class LoginController extends Controller
{
    protected $service;

    public function __construct(LoginService $service)
    {
        $this->service = $service;
    }

    public function login(LoginRequest $request)
    {
        $key = 'login:' . $request->code;
        if (!redisLock($key)) return response()->json(['code' => 0, 'msg' => '请求频繁']);
        $data = $request->validated();
        $res = $this->service->login($data);
        redisUnlock($key);
        return response()->json($res);
    }

    public function logout(Request $request)
    {
        $request->user()->tokens()->delete();
        return response()->json(['code'=>200]);
    }

    public function enter(EnterRequest $request)
    {
        $key = 'enter:' . $request->mobile;
        if (!redisLock($key)) return response()->json(['code' => 0, 'msg' => '请求频繁']);
        $data = $request->validated();
        $res = $this->service->enter($data);
        redisUnlock($key);
        return response()->json($res);
    }

    public function product()
    {
        $res = $this->service->product();
        return response()->json($res);
    }

    public function banners()
    {
        $from = request()->input('from', 1);
        $res = $this->service->banners($from);
        return response()->json($res);
    }

    public function about()
    {
        $res = $this->service->about();
        return response()->json($res);
    }

    public function sms(SmsRequest $request)
    {
        $key = 'sms:' . $request->mobile . $request->user()->id;
        if (!redisLock($key)) return response()->json(['code' => 0, 'msg' => '请求频繁']);
        $data = $request->validated();
        $res = Sms::send($data['mobile'], $data['type']);
        redisUnlock($key);
        return response()->json($res);
    }

    public function check()
    {
        $config = [
            'app_id' => config('wechat.appid'),
            'secret' => config('wechat.secret'),
            'token' => config('wechat.token'),
            'aes_key' => '', // 明文模式请勿填写 EncodingAESKey
            //ma9WLZnYPPGc8HeZkmDEHWiHcFWIhSPrJSpHrzB0uUB

            /**
             * OAuth 配置
             *
             * scopes：公众平台（snsapi_userinfo / snsapi_base），开放平台：snsapi_login
             * redirect_url：OAuth授权完成后的回调页地址
             */
            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'redirect_url' => '/examples/oauth_callback.php',
            ],

            /**
             * 接口请求相关配置，超时时间等，具体可用参数请参考：
             * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php
             */
            'http' => [
                'timeout' => 5.0,
                // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用，根据不同的模块配置不同的 uri

                'retry' => true, // 使用默认重试配置
                //  'retry' => [
                //      // 仅以下状态码重试
                //      'status_codes' => [429, 500]
                //       // 最大重试次数
                //      'max_retries' => 3,
                //      // 请求间隔 (毫秒)
                //      'delay' => 1000,
                //      // 如果设置，每次重试的等待时间都会增加这个系数
                //      // (例如. 首次:1000ms; 第二次: 3 * 1000ms; etc.)
                //      'multiplier' => 3
                //  ],
            ],
        ];

        $app = new Application($config);
        $server = $app->getServer();
        return $server->serve();
    }
}
